//
//  MarketBaseController.m
//  Fireball
//
//  Created by mba on 17/12/27.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "MarketBaseController.h"
#import "ValueModel.h"
#import "HQShiZhiCell.h"
#import "BTCvolueCell.h"
#import "BTCtopCell.h"

@interface MarketBaseController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView1;
    UITableView *_tableView2;

    UIScrollView *_scrollView;

}
@property(nonatomic, strong)NSMutableArray *aticleArray;
@property(nonatomic, assign)NSInteger pageSize;
@property(nonatomic, assign)NSInteger pageIndex;
@end

@implementation MarketBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.aticleArray = [NSMutableArray array];
    [self setupUI];
    
    self.pageSize = 20;
    self.pageIndex = 1;
    
    /** MJRefresh的block也需要用若引用 */
    if ([self.menu.Text isEqualToString:@"市值"]||[self.menu.Text isEqualToString:@"自选"]||[self.menu.Text isEqualToString:@"涨幅"]||[self.menu.Text isEqualToString:@"跌幅"]) {

        WEAKSELF
        _tableView1.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
            self.pageIndex = 1;
            [self.aticleArray removeAllObjects];
            [weakSelf getMarketValueInfo];
        }];
        //默认block方法：设置上拉加载更多
        _tableView1.mj_footer = [XMRefreshFooter xm_footerWithRefreshingBlock:^{
            self.pageIndex += 1;
            [weakSelf getMarketValueInfo];
        }];
        [_tableView1.mj_header beginRefreshing];

    }
    else{
        
    
        WEAKSELF
        _scrollView.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
            self.pageIndex = 1;
            [self.aticleArray removeAllObjects];
            [weakSelf getBTCValueInfo];
        }];
        //默认block方法：设置上拉加载更多
        _scrollView.mj_footer = [XMRefreshFooter xm_footerWithRefreshingBlock:^{
            self.pageIndex += 1;
            [weakSelf getBTCValueInfo];
        }];
        [_scrollView.mj_header beginRefreshing];
    }
}

-(void)getMarketValueInfo{
    NSDictionary * dict = @{@"PageInfo":@{@"CurrentPage":@(self.pageIndex),
                                          @"PageSize":@(self.pageSize)
                                          }
                            };
    [FBNetworkTool get:@"quotationapi/com.getMarketValueInfo" parameters:dict showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        [_tableView1 reloadData];
        
    } success:^(XMHttpResponseModel *responseModel) {
        [_scrollView.mj_header endRefreshing];
        [_scrollView.mj_footer endRefreshing];
        
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        if (array.count == 0) {
            [_tableView1.mj_footer endRefreshingWithNoMoreData];
        }
        
        [_tableView1 reloadData];
        
    } failure:^(NSError *error) {
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
    }];
    
}
-(void)getBTCValueInfo{
    NSDictionary * dict = @{@"PageInfo":@{@"CurrentPage":@(self.pageIndex),
                                          @"PageSize":@(self.pageSize)
                                          },
                            @"CID":@(self.menu.menuId),
                            @"TID":@(0),
                            @"iType":@"0"
                            };
    
    [FBNetworkTool get:@"quotationapi/com.GetTradePlantInfoAllQuotationByCurrencyInfo" parameters:dict showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        [_tableView1 reloadData];
        
    } success:^(XMHttpResponseModel *responseModel) {
        [_scrollView.mj_header endRefreshing];
        [_scrollView.mj_footer endRefreshing];
        
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.aticleArray addObjectsFromArray:array];
        if (array.count == 0) {
            [_tableView1.mj_footer endRefreshingWithNoMoreData];
        }
        
        [_tableView1 reloadData];
        
    } failure:^(NSError *error) {
        [_tableView1.mj_header endRefreshing];
        [_tableView1.mj_footer endRefreshing];
        
    }];
    
}
-(void)setupUI{
    if ([self.menu.Text isEqualToString:@"市值"]||[self.menu.Text isEqualToString:@"自选"]||[self.menu.Text isEqualToString:@"涨幅"]||[self.menu.Text isEqualToString:@"跌幅"]) {
        UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, -40, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight +40 ) style:UITableViewStyleGrouped];
        _tableView1 = tableView1;
        tableView1.tag = 1;
        tableView1.dataSource = self;
        tableView1.delegate = self;
        //    tableView1.editing=YES;
        //    tableView1.emptyDataSetSource = self;
        //    tableView1.emptyDataSetDelegate = self;
        tableView1.tableFooterView = [UIView new];
        tableView1.backgroundColor = [UIColor clearColor];
        tableView1.separatorColor = kLightGrayColor;
        [self.view addSubview:tableView1];
        
    }
    else{
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight)];
        _scrollView = scrollView;
        scrollView.scrollsToTop = NO;
        scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        scrollView.pagingEnabled = NO;
        scrollView.delegate = self;
        //    scrollView.bounces = NO;
        [self.view addSubview:scrollView];
        //
        UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 66, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight +40) style:UITableViewStyleGrouped];
        _tableView1 = tableView1;
        tableView1.tag = 1;
        tableView1.dataSource = self;
        tableView1.delegate = self;
        //    tableView1.editing=YES;
        //    tableView1.emptyDataSetSource = self;
        //    tableView1.emptyDataSetDelegate = self;
        tableView1.tableFooterView = [UIView new];
        tableView1.backgroundColor = [UIColor clearColor];
        tableView1.separatorColor = kLightGrayColor;
        [scrollView addSubview:tableView1];
        
        NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"BTCtopView" owner:nil options:nil];
        UIView *headView = [nibContents lastObject];
        headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 106);
        
        UITableView *tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(0, -40, SCREEN_WIDTH, 146) style:UITableViewStyleGrouped];
        _tableView2 = tableView2;
        tableView2.tag = 2;
        tableView2.dataSource = self;
        tableView2.delegate = self;
        //    tableView1.editing=YES;
        //    tableView1.emptyDataSetSource = self;
        //    tableView1.emptyDataSetDelegate = self;
//        tableView2.tableFooterView = [UIView new];
        tableView2.backgroundColor = [UIColor clearColor];
        tableView2.separatorColor = kLightGrayColor;
        //        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        //        view.backgroundColor = [UIColor blueColor];
        [scrollView addSubview:tableView2];
        

    }
    
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_tableView1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        if ([self.menu.Text isEqualToString:@"BTC"]) {
//            make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(60, 0, 0, 0));
//
//        }
//        else{
//        make.edges.mas_equalTo(self.view).insets(UIEdgeInsetsMake(-40, 0, 0, 0));
//        }
    }];
}
//tableview delegate
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 100;
//}
//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
//    view.backgroundColor = [UIColor blueColor];
//    return view;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag==1) {
        return [self.aticleArray count];
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    HangQingCell *cell = [HangQingCell cellWithTableView:tableView];
    //    //        cell.model=[_myCards objectAtIndex:indexPath.row];
    //    return cell;
    if (tableView.tag==1) {

    if ([self.menu.Text isEqualToString:@"市值"]||[self.menu.Text isEqualToString:@"自选"]||[self.menu.Text isEqualToString:@"涨幅"]||[self.menu.Text isEqualToString:@"跌幅"]) {
        HQShiZhiCell *cell = [HQShiZhiCell cellWithTableView:tableView];
        cell.model=[self.aticleArray objectAtIndex:indexPath.row];
        return cell;
    }
    else{
        BTCvolueCell *cell = [BTCvolueCell cellWithTableView:tableView];
        cell.model=[self.aticleArray objectAtIndex:indexPath.row];
        return cell;
    }
    }
    else{
        BTCtopCell *cell = [BTCtopCell cellWithTableView:tableView];
//        cell.model=[self.aticleArray objectAtIndex:indexPath.row];
        return cell;
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag==1) {

    return 64;
    }
    else{
        return 106;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        if (tableView.tag == 1) {
//            return 10;
//        }else {
//            return 5;
//        }
//    }else {
//        return 0.01;
//    }
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    if (tableView.tag == 1) {
    //        [MobClick event:@"mycollect_carddetail"];
    //        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //        CardDetailController *vc = [[CardDetailController alloc] init];
    //        vc.shareId = [_myCards[indexPath.row] shareid];
    //        vc.block = ^ {
    //            [tableView.mj_header beginRefreshing];
    //        };
    //        [self.navigationController pushViewController:vc animated:YES];
    //    }else {
    //        [MobClick event:@"mycollect_themedetail"];
    //        TopicDetailController *vc = [[TopicDetailController alloc] init];
    //        vc.huatiModel = _myHuatis[indexPath.row];
    //        vc.block = ^ {
    //            [tableView.mj_header beginRefreshing];
    //        };
    //        [self.navigationController pushViewController:vc animated:YES];
    //    }
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return UITableViewCellEditingStyleDelete;
    }else{
        return UITableViewCellEditingStyleInsert;
    }
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleInsert) {
        //进行数据插入操作
    }else if(editingStyle == UITableViewCellEditingStyleDelete){
        //进行删除数据操作
        [self addSelfBTCInfo:[self.aticleArray objectAtIndex:indexPath.row]];
    }
    
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==2) {
        return  @"加自选";
    }
    else{
    return @"删除";
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //NSLog(@"tracking:%d",scrollView.tracking);
    NSLog(@"%s",__func__);
//    _tableView1.frame=CGRectMake(0, 106, SCREEN_WIDTH, SCREEN_HEIGHT-kNav_Height - kTabBarHeight) ;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)addSelfBTCInfo:(ValueModel* )mode {
    NSDictionary * dict = @{@"PageInfo":@{@"CurrentPage":@(self.pageIndex),
                                          @"PageSize":@(self.pageSize)
                                          },
                            @"CID":@(mode.CID),
                            @"TID":@(mode.TID),
                            @"TWay":mode.TWay
                            };
    
    [FBNetworkTool get:@"quotationapi/com.AddUserOptional" parameters:dict showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
//        NSArray * array = [ValueModel mj_objectArrayWithKeyValuesArray:responseModel.data];
//        [self.aticleArray addObjectsFromArray:array];
//        [_tableView1 reloadData];
        
    } success:^(XMHttpResponseModel *responseModel) {
    } failure:^(NSError *error) {
    }];
    
}
@end
